$(()=>{
    let code;
    $("#sendMail").click(()=>{
        $("#first").addClass("el-loading");
        let email=$("#email").val();
        $.post("{LINK_forget/sendMail}",{
            email:email
        },text=>{
            $("#first").removeClass("el-loading");
            let data=JSON.parse(text);
            if(data.status){
                code=data.code;
                dialog("提示","邮件发送成功，请检查您的邮箱。");
            }else{
                dialog("错误","邮件发送失败");
            }
        })
    });
    $("#nextBtn").click(()=>{
        $("#first").addClass("el-loading");
        let username=$("#username").val();
        let email=$("#email").val();
        let code_val=$("#code").val();
        if(code&&code_val==code){
            $.post("{LINK_forget/verify}",{
                username:username,
                email:email
            },text=>{
                $("#first").removeClass("el-loading");
                let data=JSON.parse(text);
                if(data.status){
                    $("#first").hide();
                    $("#next").show();
                    dialog("提示","验证成功，请设置新密码。");
                }else{
                    dialog("提示",data.msg);
                }
            });
        }else{
            $("#first").removeClass("el-loading");
            dialog("错误","验证码不正确");
        }
    });
    $("#finish").click(()=>{
        $("#next").addClass("el-loading");
        let password=$("#password").val();
        let confirm=$("#confirm").val();
        let username=$("#username").val();
        if(password==confirm){
            if(password.length>=6&&password.length<=25){
                $.post("{LINK_forget/setPassword}",{
                    username:username,
                    password:password
                },text=>{
                    $("#next").removeClass("el-loading");
                    let data=JSON.parse(text);
                    if(data.status){
                        dialog("提示","密码修改成功！");
                        setTimeout(() => {
                            location.href="{LINK_login}";
                        }, 1000);
                    }else{
                        dialog("错误","密码修改失败："+data.msg);
                    }
                });
            }else{
                dialog("错误","密码必须为6-25个字符");
            }
        }else{
            dialog("错误","两次密码不一致")
        }
    });
});